//
//  UILabel+YYAdd.m
//  Commom
//
//  Created by 山刘 on 2019/3/27.
//  Copyright © 2019年 山刘. All rights reserved.
//

#import "UILabel+Bussi.h"
#import "SEGBasicBussinessCommonDefine.h"

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

@implementation UILabel (Bussi)
+(UILabel*)labelWithFrame:(CGRect)frame
                 WithFont:(UIFont*)font
                  WithTag:(NSInteger)tag
      WithBackgroundColor:(UIColor*)backgroundColor
            WithTextColor:(UIColor*)textColor
        WithTextAlignment:(NSTextAlignment)textAlignment
                 WithText:(NSString*)text
          WithBorderColor:(CGColorRef)borderColor
          WithBorderWidth:(CGFloat)borderWidth{
    
    UILabel *lb = [[UILabel alloc]initWithFrame:frame];
    if (font) {
        [lb setFont:font];
    }
    
    if (tag && tag > 0) {
        lb.tag = tag;
    }
    
    if (backgroundColor) {
        [lb setBackgroundColor:backgroundColor];
    }
    
    if (textColor) {
        [lb setTextColor:textColor];
    }
    
    if (text) {
        [lb setText:text];
    }
    
    if (borderColor) {
        lb.layer.borderColor = borderColor;
    }
    
    if (borderWidth && borderWidth > 0) {
        lb.layer.borderWidth = borderWidth;
    }
    
    [lb setTextAlignment:textAlignment];
    
    return lb;
}

+(UILabel*)labelWithFrame:(CGRect)frame
                 WithFont:(UIFont*)font
                  WithTag:(NSInteger)tag
            WithTextColor:(UIColor*)textColor
        WithTextAlignment:(NSTextAlignment)textAlignment
                 WithText:(NSString*)text{
    
    return [UILabel labelWithFrame:frame WithFont:font WithTag:tag WithBackgroundColor:nil WithTextColor:textColor WithTextAlignment:textAlignment WithText:text WithBorderColor:nil WithBorderWidth:0];
}

-(void)labelWithBorderColor:(CGColorRef)borderColor
            WithBorderWidth:(CGFloat)borderWidth{
    if (borderColor) {
        self.layer.borderColor = borderColor;
    }
    
    if (borderWidth && borderWidth > 0) {
        self.layer.borderWidth = borderWidth;
    }
}

- (void)userAuthorWithcode:(NSString*)isAuth
{
    if ([isAuth isEqualToString:@"0"]) {
        [self setText:@"未认证"];
        [self setHidden:YES];
    }

    else if ([isAuth isEqualToString:@"1"]) {

        [self setText:@"认证住户"];
        self.backgroundColor = kColor_T;
        [self setHidden:NO];
    }
    else if ([isAuth isEqualToString:@"2"]) {
        [self setText:@"认证服务者"];
        self.backgroundColor = kColor_T;
        [self setHidden:NO];
    }
    else if ([isAuth isEqualToString:@"3"]) {
        [self setText:@"官方"];
        self.backgroundColor = kColor_T;
        [self setHidden:NO];
    }
}

//TITLE_WIDTH固定宽度
-(void)extendAttributedString:(NSString*)text lineSpace:(CGFloat)lineSpace font:(UIFont *)font
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    if ([self isMoreThanOneLineWithSize:CGSizeMake(TITLE_WIDTH, 0) font:font lineSpaceing:lineSpace text:text]) {
        [paragraphStyle setLineSpacing:lineSpace];
    }
    NSDictionary *dictAttr = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font};
    NSMutableAttributedString *attributedStr=[[NSMutableAttributedString alloc] initWithString:text attributes:dictAttr];
    self.attributedText=attributedStr;
}

/**
 *  计算是否超过一行   用于给Label 赋值attribute text的时候 超过一行设置lineSpace
 */
- (BOOL)isMoreThanOneLineWithSize:(CGSize)size font:(UIFont *)font lineSpaceing:(CGFloat)lineSpacing text:(NSString*)text{
    
    if ( [self boundingRectWithSize:size font:font lineSpacing:lineSpacing text:text].height > font.lineHeight  ) {
        return YES;
    }else{
        return NO;
    }
}

/**
 * 计算文字高度，可以处理计算带行间距的
 */
- (CGSize)boundingRectWithSize:(CGSize)size font:(UIFont*)font  lineSpacing:(CGFloat)lineSpacing text:(NSString*)text
{
    // 异常处理
    if (text == nil) {
        text = @"";
    }
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
    [attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, text.length)];
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [attributeString boundingRectWithSize:size options:options context:nil];
    
    //文本的高度减去字体高度小于等于行间距，判断为当前只有1行
    if ((rect.size.height - font.lineHeight) <= paragraphStyle.lineSpacing) {
        if ([self containChinese:text]) {  //如果包含中文
            rect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height-paragraphStyle.lineSpacing);
        }
    }
    return rect.size;
}

//判断如果包含中文
- (BOOL)containChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){ int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff){
            return YES;
        }
    }
    return NO;
}

- (void)extendAttributedString:(NSString *)text lineSpace:(CGFloat)lineSpace font:(UIFont *)font color:(nonnull UIColor *)color lineBreakMode:(NSLineBreakMode)lineBreakMode {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = lineBreakMode;
    [paragraphStyle setLineSpacing:lineSpace];
    NSDictionary *dictAttr = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font,
                               NSForegroundColorAttributeName:color
                               };
    text = [SEGUtils ValueToString:text];
    NSMutableAttributedString *attributedStr=[[NSMutableAttributedString alloc] initWithString:text attributes:dictAttr];

    self.attributedText=attributedStr;
}

-(void)extendAttributedString:(NSString*)text lineSpace:(CGFloat)lineSpace font:(UIFont *)font color:(UIColor *)color
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    [paragraphStyle setLineSpacing:lineSpace];
    NSDictionary *dictAttr = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font,
                               NSForegroundColorAttributeName:color
                               };
    text = [SEGUtils ValueToString:text];
    NSMutableAttributedString *attributedStr=[[NSMutableAttributedString alloc] initWithString:text attributes:dictAttr];

    self.attributedText=attributedStr;
}

-(void)extendAttributedString:(NSString*)text lineSpace:(CGFloat)lineSpace font:(UIFont *)font width:(CGFloat)width
{
    // 异常处理
    if (text == nil) {
        text = @"";
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    if ([self isMoreThanOneLineWithSize:CGSizeMake(width, 0) font:font lineSpaceing:lineSpace text:text]) {
        [paragraphStyle setLineSpacing:lineSpace];
    }
    NSDictionary *dictAttr = @{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:font};
    NSMutableAttributedString *attributedStr=[[NSMutableAttributedString alloc] initWithString:text attributes:dictAttr];
    self.attributedText=attributedStr;
}

@end
